/*-------------------------------------------------------------------------
 *
 * spgdesc.c
 *	  rmgr 描述例程，用于 access/spgist/spgxlog.c
 *
 * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  src/backend/access/rmgrdesc/spgdesc.c
 *
 *-------------------------------------------------------------------------
 */
#include "postgres.h"

#include "access/spgxlog.h"

void spg_desc(StringInfo fc_buf, XLogReaderState *fc_record)
{
	char	   *fc_rec = XLogRecGetData(fc_record);
	uint8		fc_info = XLogRecGetInfo(fc_record) & ~XLR_INFO_MASK;

	switch (fc_info)
	{
		case XLOG_SPGIST_ADD_LEAF:
			{
				spgxlogAddLeaf *fc_xlrec = (spgxlogAddLeaf *) fc_rec;

				appendStringInfo(fc_buf, "off: %u, headoff: %u, parentoff: %u, nodeI: %u",
								 fc_xlrec->offnumLeaf, fc_xlrec->offnumHeadLeaf,
								 fc_xlrec->offnumParent, fc_xlrec->nodeI);
				if (fc_xlrec->newPage)
					appendStringInfoString(fc_buf, " (newpage)");
				if (fc_xlrec->storesNulls)
					appendStringInfoString(fc_buf, " (nulls)");
			}
			break;
		case XLOG_SPGIST_MOVE_LEAFS:
			{
				spgxlogMoveLeafs *fc_xlrec = (spgxlogMoveLeafs *) fc_rec;

				appendStringInfo(fc_buf, "nmoves: %u, parentoff: %u, nodeI: %u",
								 fc_xlrec->nMoves,
								 fc_xlrec->offnumParent, fc_xlrec->nodeI);
				if (fc_xlrec->newPage)
					appendStringInfoString(fc_buf, " (newpage)");
				if (fc_xlrec->replaceDead)
					appendStringInfoString(fc_buf, " (replacedead)");
				if (fc_xlrec->storesNulls)
					appendStringInfoString(fc_buf, " (nulls)");
			}
			break;
		case XLOG_SPGIST_ADD_NODE:
			{
				spgxlogAddNode *fc_xlrec = (spgxlogAddNode *) fc_rec;

				appendStringInfo(fc_buf, "off: %u, newoff: %u, parentBlk: %d, "
								 "parentoff: %u, nodeI: %u",
								 fc_xlrec->offnum,
								 fc_xlrec->offnumNew,
								 fc_xlrec->parentBlk,
								 fc_xlrec->offnumParent,
								 fc_xlrec->nodeI);
				if (fc_xlrec->newPage)
					appendStringInfoString(fc_buf, " (newpage)");
			}
			break;
		case XLOG_SPGIST_SPLIT_TUPLE:
			{
				spgxlogSplitTuple *fc_xlrec = (spgxlogSplitTuple *) fc_rec;

				appendStringInfo(fc_buf, "prefixoff: %u, postfixoff: %u",
								 fc_xlrec->offnumPrefix,
								 fc_xlrec->offnumPostfix);
				if (fc_xlrec->newPage)
					appendStringInfoString(fc_buf, " (newpage)");
				if (fc_xlrec->postfixBlkSame)
					appendStringInfoString(fc_buf, " (same)");
			}
			break;
		case XLOG_SPGIST_PICKSPLIT:
			{
				spgxlogPickSplit *fc_xlrec = (spgxlogPickSplit *) fc_rec;

				appendStringInfo(fc_buf, "ndelete: %u, ninsert: %u, inneroff: %u, "
								 "parentoff: %u, nodeI: %u",
								 fc_xlrec->nDelete, fc_xlrec->nInsert,
								 fc_xlrec->offnumInner,
								 fc_xlrec->offnumParent, fc_xlrec->nodeI);
				if (fc_xlrec->innerIsParent)
					appendStringInfoString(fc_buf, " (innerIsParent)");
				if (fc_xlrec->storesNulls)
					appendStringInfoString(fc_buf, " (nulls)");
				if (fc_xlrec->isRootSplit)
					appendStringInfoString(fc_buf, " (isRootSplit)");
			}
			break;
		case XLOG_SPGIST_VACUUM_LEAF:
			{
				spgxlogVacuumLeaf *fc_xlrec = (spgxlogVacuumLeaf *) fc_rec;

				appendStringInfo(fc_buf, "ndead: %u, nplaceholder: %u, nmove: %u, nchain: %u",
								 fc_xlrec->nDead, fc_xlrec->nPlaceholder,
								 fc_xlrec->nMove, fc_xlrec->nChain);
			}
			break;
		case XLOG_SPGIST_VACUUM_ROOT:
			{
				spgxlogVacuumRoot *fc_xlrec = (spgxlogVacuumRoot *) fc_rec;

				appendStringInfo(fc_buf, "ndelete: %u",
								 fc_xlrec->nDelete);
			}
			break;
		case XLOG_SPGIST_VACUUM_REDIRECT:
			{
				spgxlogVacuumRedirect *fc_xlrec = (spgxlogVacuumRedirect *) fc_rec;

				appendStringInfo(fc_buf, "ntoplaceholder: %u, firstplaceholder: %u, newestredirectxid: %u",
								 fc_xlrec->nToPlaceholder,
								 fc_xlrec->firstPlaceholder,
								 fc_xlrec->newestRedirectXid);
			}
			break;
	}
}

const char * spg_identify(uint8 fc_info)
{
	const char *fc_id = NULL;

	switch (fc_info & ~XLR_INFO_MASK)
	{
		case XLOG_SPGIST_ADD_LEAF:
			fc_id = "ADD_LEAF";
			break;
		case XLOG_SPGIST_MOVE_LEAFS:
			fc_id = "MOVE_LEAFS";
			break;
		case XLOG_SPGIST_ADD_NODE:
			fc_id = "ADD_NODE";
			break;
		case XLOG_SPGIST_SPLIT_TUPLE:
			fc_id = "SPLIT_TUPLE";
			break;
		case XLOG_SPGIST_PICKSPLIT:
			fc_id = "PICKSPLIT";
			break;
		case XLOG_SPGIST_VACUUM_LEAF:
			fc_id = "VACUUM_LEAF";
			break;
		case XLOG_SPGIST_VACUUM_ROOT:
			fc_id = "VACUUM_ROOT";
			break;
		case XLOG_SPGIST_VACUUM_REDIRECT:
			fc_id = "VACUUM_REDIRECT";
			break;
	}

	return fc_id;
}
